--region [dbo].[AddCurrentWeight]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[AddCurrentWeight]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[AddCurrentWeight]') IS NOT NULL
	DROP PROCEDURE [dbo].[AddCurrentWeight]
GO

CREATE PROCEDURE [dbo].[AddCurrentWeight]
	@PRId int,
	@DataValue float,
	@WeightHistoryId int
AS

INSERT INTO [PatientCurrentWeight] (
	[PRId],
	[DataValue],
	[WeightHistoryId]
) VALUES (
	@PRId,
	@DataValue,
	@WeightHistoryId
)

GO
--endregion


--region [dbo].[UpdateCurrentWeight]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[UpdateCurrentWeight]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[UpdateCurrentWeight]') IS NOT NULL
	DROP PROCEDURE [dbo].[UpdateCurrentWeight]
GO

CREATE PROCEDURE [dbo].[UpdateCurrentWeight]
	@PRId int,
	@DataValue float,
	@WeightHistoryId int
AS

UPDATE [PatientCurrentWeight] SET
	[DataValue] = @DataValue,
	[WeightHistoryId] = @WeightHistoryId
WHERE
	[PRId] = @PRId

GO
--endregion


--region [dbo].[DelCurrentWeight]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[DelCurrentWeight]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[DelCurrentWeight]') IS NOT NULL
	DROP PROCEDURE [dbo].[DelCurrentWeight]
GO

CREATE PROCEDURE [dbo].[DelCurrentWeight]
	@WhereCondition nvarchar(512)
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
DELETE FROM
	[PatientCurrentWeight]
WHERE
	' + @WhereCondition

EXEC sp_executesql @SQL

GO
--endregion


--region [dbo].[GetCurrentWeight]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[GetCurrentWeight]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[GetCurrentWeight]') IS NOT NULL
	DROP PROCEDURE [dbo].[GetCurrentWeight]
GO

CREATE PROCEDURE [dbo].[GetCurrentWeight]
	@WhereCondition nvarchar(512)
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT
	''Id'' = [PatientWeightHistory].[Id],
	''PRId'' = [PatientCurrentWeight].[PRId],
	''DataValue'' = [PatientCurrentWeight].[DataValue],
	[RecordDate],
	[UpdateDate]
FROM
	[PatientCurrentWeight], [PatientWeightHistory]
WHERE
	[PatientCurrentWeight].[WeightHistoryId] = [PatientWeightHistory].[Id] AND
	' + @WhereCondition

EXEC sp_executesql @SQL

GO
--endregion


--region [dbo].[GetCurrentWeightCount]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[GetCurrentWeightCount]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[GetCurrentWeightCount]') IS NOT NULL
	DROP PROCEDURE [dbo].[GetCurrentWeightCount]
GO

CREATE PROCEDURE [dbo].[GetCurrentWeightCount]
	@WhereCondition nvarchar(512)
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT 
	COUNT(Id)
FROM
	[PatientCurrentWeight]
WHERE
	' + @WhereCondition

EXEC sp_executesql @SQL

GO
--endregion


--region [dbo].[AddWeightHistory]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[AddWeightHistory]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[AddWeightHistory]') IS NOT NULL
	DROP PROCEDURE [dbo].[AddWeightHistory]
GO

CREATE PROCEDURE [dbo].[AddWeightHistory]
	@PRId int,
	@DataValue float,
	@RecordDate datetime,
	@UpdateDate datetime,
	@id int OUTPUT
AS

INSERT INTO [PatientWeightHistory] (
	[PRId],
	[DataValue],
	[RecordDate],
	[UpdateDate]
) VALUES (
	@PRId,
	@DataValue,
	@RecordDate,
	@UpdateDate
)

SET @id = SCOPE_IDENTITY()

GO
--endregion


--region [dbo].[UpdateWeightHistory]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[UpdateWeightHistory]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[UpdateWeightHistory]') IS NOT NULL
	DROP PROCEDURE [dbo].[UpdateWeightHistory]
GO

CREATE PROCEDURE [dbo].[UpdateWeightHistory]
	@Id int,
	@PRId int,
	@DataValue float,
	@RecordDate datetime,
	@UpdateDate datetime
AS

UPDATE [PatientWeightHistory] SET
	[DataValue] = @DataValue,
	[RecordDate] = @RecordDate,
	[UpdateDate] = @UpdateDate
WHERE
	[PRId] = @PRId  AND [Id] = @Id 
GO
--endregion


--region [dbo].[DelWeightHistory]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[DelWeightHistory]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[DelWeightHistory]') IS NOT NULL
	DROP PROCEDURE [dbo].[DelWeightHistory]
GO

CREATE PROCEDURE [dbo].[DelWeightHistory]
	@WhereCondition nvarchar(512)
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
DELETE FROM
	[PatientWeightHistory]
WHERE
	' + @WhereCondition

EXEC sp_executesql @SQL

GO
--endregion


--region [dbo].[GetWeightHistory]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[GetWeightHistory]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[GetWeightHistory]') IS NOT NULL
	DROP PROCEDURE [dbo].[GetWeightHistory]
GO

CREATE PROCEDURE [dbo].[GetWeightHistory]
	@WhereCondition nvarchar(512)
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT
	''Id'' = [PatientWeightHistory].[Id],
	[PRId],
	[DataValue],
	[RecordDate],
	[UpdateDate]
FROM
	[PatientWeightHistory]
WHERE
	' + @WhereCondition

EXEC sp_executesql @SQL

GO
--endregion


--region [dbo].[GetWeightHistoryList]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[GetWeightHistoryList]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[GetWeightHistoryList]') IS NOT NULL
	DROP PROCEDURE [dbo].[GetWeightHistoryList]
GO

CREATE PROCEDURE [dbo].[GetWeightHistoryList]
	@PageSize int,
	@PageIndex int,
	@WhereCondition nvarchar(512),
	@OrderByExpression nvarchar(250) = NULL
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT 
	COUNT(Id) 
FROM [PatientWeightHistory]
	'
IF @WhereCondition <> ''
BEGIN
	SET @SQL = @SQL + ' WHERE ' + @WhereCondition
END 

EXEC sp_executesql @SQL

SET @SQL = '
SELECT TOP ' + STR(@pagesize) + '
	''Id'' = [PatientWeightHistory].[Id],
	[PRId],
	[DataValue],
	[RecordDate],
	[UpdateDate]
FROM
	[PatientWeightHistory]
	'

IF @PageIndex > 1
BEGIN
SET @SQL = @SQL + '
	WHERE [PatientWeightHistory].[Id] < 
	( 
	SELECT MIN([Id])  
	FROM ( 
	SELECT TOP 
		' + STR((@PageIndex-1)*@PageSize) + ' [Id] 
	FROM [PatientWeightHistory] ' 

	IF @WhereCondition <> ''
	BEGIN
		SET @SQL = @SQL + ' WHERE ' + @WhereCondition
	END

	IF @OrderByExpression <> ''
	BEGIN
		SET @SQL = @SQL + '
	ORDER BY
		' + @OrderByExpression + ') AS tblTmp ) '
	END
	ELSE
	BEGIN
		SET @SQL = @SQL + ') AS tblTmp ) '
	END

	IF @WhereCondition <> ''
	BEGIN
	SET @SQL = @SQL + ' AND ' + @WhereCondition
	END
	
	IF @OrderByExpression <> ''
	BEGIN
		SET @SQL = @SQL + '
	ORDER BY
		' + @OrderByExpression 
	END	
END
ELSE 
BEGIN
	IF @WhereCondition <> ''
	BEGIN
	SET @SQL = @SQL + ' WHERE ' + @WhereCondition
	END
	
	IF @OrderByExpression <> ''
	BEGIN
		SET @SQL = @SQL + ' 
	ORDER BY
		' + @OrderByExpression 
	END	
END

EXEC sp_executesql @SQL

GO
--endregion


--region [dbo].[GetAllWeightHistory]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[GetAllWeightHistory]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[GetAllWeightHistory]') IS NOT NULL
	DROP PROCEDURE [dbo].[GetAllWeightHistory]
GO

CREATE PROCEDURE [dbo].[GetAllWeightHistory]
	@WhereCondition nvarchar(512),
	@OrderByExpression nvarchar(250) = NULL
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT
	''Id'' = [PatientWeightHistory].[Id],
	[PRId],
	[DataValue],
	[RecordDate],
	[UpdateDate]
FROM
	[PatientWeightHistory]
	'

IF @WhereCondition <> ''
BEGIN
	SET @SQL = @SQL + ' WHERE ' + @WhereCondition
END 

IF @OrderByExpression <> ''
BEGIN
	SET @SQL = @SQL + '
ORDER BY
	' + @OrderByExpression 
END	

EXEC sp_executesql @SQL

GO
--endregion



--region [dbo].[GetWeightHistoryCount]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[GetWeightHistoryCount]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[GetWeightHistoryCount]') IS NOT NULL
	DROP PROCEDURE [dbo].[GetWeightHistoryCount]
GO

CREATE PROCEDURE [dbo].[GetWeightHistoryCount]
	@WhereCondition nvarchar(512)
AS

DECLARE @SQL nvarchar(3250)

SET @SQL = '
SELECT 
	COUNT(Id)
FROM
	[PatientWeightHistory]
WHERE
	' + @WhereCondition

EXEC sp_executesql @SQL

GO
--endregion


--region [dbo].[UpdatePatientHeight]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[UpdatePatientHeight]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[UpdatePatientHeight]') IS NOT NULL
	DROP PROCEDURE [dbo].[UpdatePatientHeight]
GO

CREATE PROCEDURE [dbo].[UpdatePatientHeight]
	@Id int,
	@Height float
AS

UPDATE [UserInfo] SET
	[Height] = @Height
WHERE
	[Id] = @Id 
GO
--endregion


--region [dbo].[GetPatientHeight]

------------------------------------------------------------------------------------------------------------------------
-- Template:       StoredProcedres.cst
-- Procedure Name: [dbo].[UpdatePatientHeight]
------------------------------------------------------------------------------------------------------------------------

IF OBJECT_ID(N'[dbo].[GetPatientHeight]') IS NOT NULL
	DROP PROCEDURE [dbo].[GetPatientHeight]
GO

CREATE PROCEDURE [dbo].[GetPatientHeight]
	@Id int
AS
SELECT 
	[Height]
From 
	[UserInfo]
WHERE
	[Id] = @Id 
GO
--endregion